package com.pl.lc.user.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pl.lc.common.config.SystemConfig;
import com.pl.lc.common.param.PayAdd;
import com.pl.lc.common.param.UserVipAdd;
import com.pl.lc.common.util.JwtUtil;
import com.pl.lc.common.vo.R;
import com.pl.lc.user.entity.Vip;
import com.pl.lc.user.mapper.VipMapper;
import com.pl.lc.user.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 8.用户收费会员表 前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-11
 */
@RestController
@RequestMapping("/user/vip")
public class VipController {


    @Autowired
    private VipService vipService;
    @Autowired
    private VipMapper vipMapper;

    @RequestMapping("/save")
    public R save(HttpServletRequest request ,@RequestBody UserVipAdd userVipAdd){
        //判断参数不能为空
        if (userVipAdd == null){
            return R.fail("参数为空");
        }
        //获取用户id（两种方式1,mapper查数据库，2解析token 这里用到解析token）
        String token =request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat)JwtUtil.parseToken(token, "id")).intValue();
        userVipAdd.setUserId(id);
        //根据不同的参数类型，经过不同的支付途径
        switch (userVipAdd.getPayType()){
            //钱包支付
            case 1:
                return vipService.buyVipByWallet(userVipAdd);
            //支付宝支付
            case 2:
                return vipService.buyVipByZfb(userVipAdd);
            //微信支付
            case 3:
                break;

        }
        return R.fail("开通失败");
    }

    @RequestMapping("/my")
    public R my(HttpServletRequest request){

        String token =request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat)JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<Vip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        Vip vip = vipMapper.selectOne(queryWrapper);
        if (vip!=null){
            return  R.ok(vip);
        }
        return R.fail("展示页面信息失败");
    }
}

